<template>
	<view class="page-caregiver-login">
		<!-- 状态栏占位 -->
		<!-- <view class="status-bar"></view> -->
		
		<!-- 背景图片 -->
		<view class="background-container">
			<image class="bg-image" src="/static/loginss/Group 1707487576.png" mode="aspectFill"></image>
			

		</view>
		
		<!-- 登录表单悬浮卡片 -->
		<view class="login-form-card">
			<!-- 账号登录 -->
			<view class="input-section">
				<view class="input-header">
					<image class="input-icon" src="/static/loginss/Group 1707487364.png" mode="aspectFit"></image>
					<text class="input-title">账号登录</text>
				</view>
				<view class="input-container">
					<input 
						class="input-field" 
						v-model="userInfo.account" 
						placeholder="请输入您的邮箱/手机号/身份证号"
						placeholder-class="placeholder-style"
					/>
				</view>
			</view>
			
			<!-- 密码登录 -->
			<view class="input-section">
				<view class="input-header">
					<image class="input-icon" src="/static/loginss/Group 1707487365.png" mode="aspectFit"></image>
					<text class="input-title">密码登录</text>
				</view>
				<view class="input-container">
					<input 
						class="input-field" 
						v-model="userInfo.password" 
						:type="showPassword ? 'text' : 'password'"
						placeholder="请输入登录密码"
						placeholder-class="placeholder-style"
					/>
					<view class="eye-icon" @tap="togglePasswordVisibility">
						<image 
							:src="showPassword ? '/static/loginss/Group 1707487366@2x.png' : '/static/loginss/Group 1707487366.png'" 
							mode="aspectFit"
						></image>
					</view>
				</view>
			</view>
			
			<!-- 记住密码 -->
			<view class="remember-password">
				<view class="checkbox-container" @tap="toggleRememberPassword">
					<view class="checkbox" :class="{ 'checked': rememberPassword }">
						<text v-if="rememberPassword" class="checkmark">✓</text>
					</view>
					<text class="checkbox-text">记住密码</text>
				</view>
			</view>
			
			<!-- 登录按钮 -->
			<button class="login-button" @tap="login">
				<text class="login-button-text">立即登录</text>
			</button>
			
			<!-- 协议区域 -->
			<view class="agreement-section">
				<view class="agreement-container" @tap="toggleAgreement">
					<view class="agreement-checkbox" :class="{ 'checked': agreementChecked }">
						<text v-if="agreementChecked" class="agreement-checkmark">✓</text>
					</view>
					<text class="agreement-text">已阅读并同意</text>
					<text class="agreement-link" @tap.stop="openUserAgreement">《用户使用协议》</text>
					<text class="agreement-link" @tap.stop="openPrivacyPolicy">《隐私协议》</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import { apiLogin, apiGetUserList } from '@/request/api.js'

export default {
	data() {
		return {
			userInfo: {
				account: '',
				password: ''
			},
			showPassword: false,
			rememberPassword: false,
			agreementChecked: true
		}
	},
	onLoad() {
		// 检查是否有保存的账号信息
		const rememberPassword = uni.getStorageSync('rememberPassword');
		const savedAccount = uni.getStorageSync('savedAccount');
		const savedPassword = uni.getStorageSync('savedPassword');
		
		if (rememberPassword && savedAccount) {
			this.rememberPassword = true;
			this.userInfo.account = savedAccount;
			if (savedPassword) {
				this.userInfo.password = savedPassword;
			}
		}
		
		// 检查是否已经登录
		const userInfo = uni.getStorageSync('userInfo');
		if (userInfo && userInfo.hasLogin && userInfo.token) {
			// 已经登录，直接跳转到首页
			uni.switchTab({
				url: '../tabbar/home'
			});
		}
	},
	methods: {
		// 切换密码显示/隐藏
		togglePasswordVisibility() {
			this.showPassword = !this.showPassword;
		},
		
		// 切换记住密码
		toggleRememberPassword() {
			this.rememberPassword = !this.rememberPassword;
		},
		
		// 切换协议同意
		toggleAgreement() {
			this.agreementChecked = !this.agreementChecked;
		},
		
		// 获取用户详细信息
		async getUserDetailInfo(token) {
			try {
				// 临时保存token以供API调用使用
				const tempUserInfo = {
					token: token,
					hasLogin: true
				};
				uni.setStorageSync('userInfo', tempUserInfo);
				
				// 调用用户列表API获取详细信息
				const userListResponse = await apiGetUserList({
					uid: '1' // 根据API文档使用固定值，实际可能需要从登录响应获取
				});
				
				if (userListResponse.code === 0 && userListResponse.data && userListResponse.data.info) {
					const userDetail = userListResponse.data.info;
					const userList = userListResponse.data.data || [];
					
					// 构建完整的用户信息
					const completeUserInfo = {
						// 基础登录信息
						token: token,
						hasLogin: true,
						
						// 用户详细信息
						uid: userDetail.id,
						name: userDetail.name,
						nickname: userDetail.nickname,
						phone: userDetail.phone,
						email: userDetail.email,
						
						// 个人信息
						sex: userDetail.sex,
						birthday: userDetail.birthday,
						marry: userDetail.marry,
						contact: userDetail.contact,
						contact_phone: userDetail.contact_phone,
						
						// 身份信息
						id_num: userDetail.id_num,
						id_back: userDetail.id_back,
						id_face: userDetail.id_face,
						
						// 地址信息
						province: userDetail.province,
						city: userDetail.city,
						area: userDetail.area,
						
						// 工作信息
						part: userDetail.part,
						position: userDetail.position,
						level: userDetail.level,
						
						// 健康信息
						medical_history: userDetail.medical_history,
						allergy_history: userDetail.allergy_history,
						
						// 系统信息
						status: userDetail.status,
						auth_ids: userDetail.auth_ids,
						create_time: userDetail.create_time,
						update_time: userDetail.update_time,
						
						// 其他
						photo: userDetail.photo,
						desc: userDetail.desc,
						classes: userDetail.classes,
						
						// 用户列表（如果需要）
						userList: userList,
						
						// 统计信息
						num: userDetail.num,
						ip: userDetail.ip,
						position_time: userDetail.position_time
					};
					
					// 保存完整的用户信息
					uni.setStorageSync('userInfo', completeUserInfo);
					uni.setStorageSync('hasLogin', true);
					
					console.log('用户详细信息获取成功:', completeUserInfo);
					return true;
				} else {
					console.error('获取用户详细信息失败:', userListResponse);
					return false;
				}
			} catch (error) {
				console.error('获取用户详细信息出错:', error);
				return false;
			}
		},
		
		// 登录
		async login() {
			if (!this.userInfo.account) {
				uni.showToast({
					title: '请输入账号',
					icon: 'none'
				});
				return;
			}
			
			if (!this.userInfo.password) {
				uni.showToast({
					title: '请输入密码',
					icon: 'none'
				});
				return;
			}
			
			if (!this.agreementChecked) {
				uni.showToast({
					title: '请先阅读并同意相关协议',
					icon: 'none'
				});
				return;
			}
			
			uni.showLoading({
				title: '登录中...',
				mask: true
			});
			
			try {
				// 调用登录API
				const loginData = {
					phone: this.userInfo.account,
					password: this.userInfo.password
				};
				
				const response = await apiLogin(loginData);
				
				if (response.code === 0) {
					// 登录成功，获取用户详细信息
					const userListResponse = await this.getUserDetailInfo(response.data.token);
					
					if (userListResponse) {
						uni.hideLoading();
						uni.showToast({
							title: response.msg || '登录成功',
							icon: 'success'
						});
						
						// 保存记住密码状态
						if (this.rememberPassword) {
							uni.setStorageSync('rememberPassword', true);
							uni.setStorageSync('savedAccount', this.userInfo.account);
							uni.setStorageSync('savedPassword', this.userInfo.password);
						} else {
							uni.removeStorageSync('rememberPassword');
							uni.removeStorageSync('savedAccount');
							uni.removeStorageSync('savedPassword');
						}
						
						// 跳转到首页
						setTimeout(() => {
							uni.switchTab({
								url: '../tabbar/home'
							});
						}, 1500);
					} else {
						uni.hideLoading();
						// 如果获取用户信息失败，仍然允许登录但使用基础信息
						const basicUserInfo = {
							token: response.data.token,
							phone: response.data.phone,
							flag: response.data.flag,
							hasLogin: true,
							uid: response.data.uid || '33897' // 使用返回的uid或默认值
						};
						uni.setStorageSync('userInfo', basicUserInfo);
						uni.setStorageSync('hasLogin', true);
						
						uni.showToast({
							title: '登录成功',
							icon: 'success'
						});
						
						setTimeout(() => {
							uni.switchTab({
								url: '../tabbar/home'
							});
						}, 1500);
					}
				}
			} catch (error) {
				uni.hideLoading();
				console.error('登录失败:', error);
				// 错误信息已经在request.js中通过toast显示了
			}
		},
		
		// 打开用户协议
		openUserAgreement() {
			uni.navigateTo({
				url: '../webview/webview?url=http://www.example.com/user-agreement'
			});
		},
		
		// 打开隐私政策
		openPrivacyPolicy() {
			uni.navigateTo({
				url: '../webview/webview?url=http://www.example.com/privacy-policy'
			});
		}
	}
}
</script>

<style lang="scss" scoped>
.page-caregiver-login {
	min-height: 100vh;
	position: relative;
	/* 安卓平台适配 */
	/* #ifdef APP-PLUS */
	/* #ifdef APP-PLUS-ANDROID */
	// padding-top: var(--status-bar-height);
	/* #endif */
	/* #endif */
}


.background-container {
	position: relative;
	width: 100%;
	height: 100vh;
	overflow: hidden;
}

.bg-image {
	width: 100%;
	height: 100%;
	position: absolute;
	top: 0;
	left: 0;
	z-index: 1;
}

.welcome-info {
	position: absolute;
	top: 160rpx;
	left: 60rpx;
	z-index: 2;
}

.welcome-text {
	display: flex;
	flex-direction: column;
}

.welcome-title {
	font-size: 48rpx;
	font-weight: bold;
	color: #333333;
	display: block;
	margin-bottom: 10rpx;
}

.platform-name {
	font-size: 24rpx;
	color: #999999;
	display: block;
}

.login-form-card {
	position: absolute;
	top: 66%;
	left: 50%;
	transform: translate(-50%, -50%);
	width: 700rpx;
	height: 1100rpx;
	background: linear-gradient(180deg, rgba(255, 255, 255, 0.85) 60.9%, rgba(255, 255, 255, 0) 100%);
	border: 2rpx solid #FFFFFF;
	border-radius: 20rpx;
	backdrop-filter: blur(20rpx);
	z-index: 3;
	padding: 80rpx 60rpx;
	box-sizing: border-box;
}

.input-section {
	margin-bottom: 40rpx;
}

.input-header {
	display: flex;
	align-items: center;
	margin-bottom: 20rpx;
}

.input-icon {
	width: 32rpx;
	height: 32rpx;
	margin-right: 16rpx;
}

.input-title {
	font-size: 28rpx;
	color: #333333;
	font-weight: 500;
}

.input-container {
	position: relative;
	background-color: #FFFFFF;
	border: 1rpx solid #EEEEEE;
	border-radius: 16rpx;
	padding: 0 24rpx;
	height: 88rpx;
	display: flex;
	align-items: center;
}

.input-field {
	flex: 1;
	height: 88rpx;
	font-size: 28rpx;
	color: #333333;
	background-color: transparent;
	border: none;
	outline: none;
}

.placeholder-style {
	color: #999999;
	font-size: 28rpx;
}

/* 移除输入框的默认焦点样式 */
.input-field:focus {
	outline: none;
	border: none;
	box-shadow: none;
	color: #333333;
}

.input-field::placeholder {
	color: #999999;
}

/* 安卓平台特殊处理 */
/* #ifdef APP-PLUS-ANDROID */
.input-field {
	-webkit-appearance: none;
	appearance: none;
}
/* #endif */

.eye-icon {
	width: 40rpx;
	height: 40rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}

.eye-icon image {
	width: 32rpx;
	height: 32rpx;
}

.remember-password {
	margin-bottom: 60rpx;
}

.checkbox-container {
	display: flex;
	align-items: center;
}

.checkbox {
	width: 32rpx;
	height: 32rpx;
	border: 2rpx solid #DDDDDD;
	border-radius: 6rpx;
	margin-right: 16rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	background-color: #FFFFFF;
}

.checkbox.checked {
	background-color: #FF7D00;
	border-color: #FF7D00;
}

.checkmark {
	color: #FFFFFF;
	font-size: 20rpx;
	font-weight: bold;
}

.checkbox-text {
	font-size: 26rpx;
	color: #999999;
}

.login-button {
	width: 580rpx;
	height: 74rpx;
	background: linear-gradient(271deg, #FF7D00 0%, #E60012 100%);
	border-radius: 10rpx;
	border: none;
	display: flex;
	align-items: center;
	justify-content: center;
	margin: 0 auto 40rpx;
	box-shadow: 0 6rpx 8rpx rgba(89, 0, 7, 0.12);
}

.login-button-text {
	color: #FFFFFF;
	font-size: 28rpx;
	font-weight: bold;
}

.agreement-section {
	display: flex;
	justify-content: center;
	position: absolute;
	bottom: 40rpx;
	left: 0;
	right: 0;
}

.agreement-container {
	display: flex;
	align-items: center;
	flex-wrap: wrap;
	justify-content: center;
}

.agreement-checkbox {
	width: 24rpx;
	height: 24rpx;
	border: 2rpx solid #FF7D00;
	border-radius: 4rpx;
	margin-right: 12rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	background-color: #FF7D00;
}

.agreement-checkbox.checked {
	background-color: #FF7D00;
}

.agreement-checkmark {
	color: #FFFFFF;
	font-size: 16rpx;
	font-weight: bold;
}

.agreement-text {
	font-size: 24rpx;
	color: #333333;
	margin-right: 8rpx;
}

.agreement-link {
	font-size: 24rpx;
	color: #FF7D00;
	margin: 0 4rpx;
}
</style> 